unit uSelectNode;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, cxContainer, cxEdit, dxSkinsCore, dxSkinBlack,
  dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom,
  dxSkinDarkSide, dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus, dxSkinSilver,
  dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld,
  dxSkinsDefaultPainters, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue, Vcl.Menus, cxCustomData, cxStyles, cxTL,
  cxTLdxBarBuiltInMenu, cxInplaceContainer, cxTLData, cxDBTL, Vcl.StdCtrls,
  cxButtons, cxGroupBox, Data.DB, cxCheckBox, cxMaskEdit, Datasnap.DBClient,
  ClientRowSet, Vcl.ActnList;

type
  TFrmSelectNode = class(TForm)
    cxGroupBox1: TcxGroupBox;
    cxButton11: TcxButton;
    cxButton1: TcxButton;
    cxDBTreeList1: TcxDBTreeList;
    dsNode: TDataSource;
    crsNode: TClientRowSet;
    crsNodeNODE_NO: TStringField;
    crsNodeNODE_NAME: TStringField;
    crsNodePARENT_NODE: TStringField;
    crsNodeCHECKED: TBooleanField;
    cxDBTreeList1cxDBTreeListColumn1: TcxDBTreeListColumn;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    cxDBTreeList1cxDBTreeListColumn3: TcxDBTreeListColumn;
    ActionList1: TActionList;
    actSave: TAction;
    actCancel: TAction;
    procedure actSaveExecute(Sender: TObject);
    procedure actCancelExecute(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    userId: string;
  public
    { Public declarations }
  end;

var
  FrmSelectNode: TFrmSelectNode;
function FrmSelectNodeShow(uId: string): Boolean;
implementation

{$R *.dfm}

uses uDm, ucommFunction, Console;

function FrmSelectNodeShow(uId: string): Boolean;
var
  sqlText: string;
  I: Integer;
begin
  Result := False;
  Application.CreateForm(TFrmSelectNode, FrmSelectNode);
  with FrmSelectNode do
  begin
    userId := uId;
    sqlText := 'select id,user_id, node_no from sys_role_node where user_id=' + QuotedStr(uId);
    OpenDataSet(sqlText, Dm.crsRoleNode);
    sqlText := 'select node_no,node_name,parent_node from sys_group_node where group_code=' + QuotedStr(userInfo.groupCode)+ ' ORDER BY parent_node,node_no';
    OpenDataSet(sqlText, crsNode);
    crsNode.DisableControls;
    for I := 1 to crsNode.RecordCount do
    begin
      crsNode.RecNo := I;
      crsNode.Edit;
      if dm.crsRoleNode.Locate('node_no', crsNode.FieldByName('node_no').AsString, []) then
      begin
        crsNode.FieldByName('checked').AsBoolean := True;
      end else
        crsNode.FieldByName('checked').AsBoolean := False;
      crsNode.Post;
    end;
//    crsNode.First;
    crsNode.EnableControls;
    if ShowModal = mrOk then
    begin
      Result := True;
    end else
      dm.crsRoleNode.CancelUpdates;
    FreeAndNil(FrmSelectNode);
  end;
end;

procedure TFrmSelectNode.actCancelExecute(Sender: TObject);
begin
  ModalResult := mrCancel;
end;

procedure TFrmSelectNode.actSaveExecute(Sender: TObject);
var
  I: Integer;
begin
  crsNode.DisableControls;
  for I := 1 to crsNode.RecordCount do
  begin
    crsNode.RecNo := I;
    if crsNode.FieldByName('checked').AsBoolean then
    begin
      if not dm.crsRoleNode.Locate('node_no', crsNode.FieldByName('node_no').AsString, []) then
      begin
        dm.crsRoleNode.Append;
        dm.crsRoleNode.FieldByName('id').AsString := GetGUID;
        dm.crsRoleNode.FieldByName('user_Id').AsString := userId;
        dm.crsRoleNode.FieldByName('node_no').AsString := crsNode.FieldByName('node_no').AsString;
        dm.crsRoleNode.Post;
      end;
    end else
    begin
      if dm.crsRoleNode.Locate('node_no', crsNode.FieldByName('node_no').AsString, []) then
      begin
        dm.crsRoleNode.Delete;
      end;
    end;
  end;
  dm.crsRoleNode.ApplyUpdates;
  crsNode.First;
  crsNode.EnableControls;
  ModalResult := mrOk;
end;

procedure TFrmSelectNode.FormCreate(Sender: TObject);
begin
  pSetCaption(TForm(Sender));
end;

end.
